package com.qimo.zzy.service;

import com.qimo.zzy.dao.UserDao;
import com.qimo.zzy.entity.ResultEntity;
import com.qimo.zzy.tool.MD5Tool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    //用户服务类
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);//日志。getLogger需要传入当前的类名
    @Resource
    UserDao userDao;
    @Resource
    MD5Tool md5Tool;


    public String changeAvatat(int uid, String path) {
        try {
            return ResultEntity.success(userDao.updateImg(uid, path));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据手机号查询密码
     *
     * @param phone
     * @return
     */
    public String loginByphone(String phone) {
        try {
            System.out.println(userDao.selectPwdByUphone(phone));
            return userDao.selectPwdByUphone(phone);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
    public String selectByUemail(String uemail) {
        try {
            return ResultEntity.success(userDao.selectByUemail(uemail));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
    public String selectByphone(String phone) {
        try  {return ResultEntity.success(userDao.selectByphone(phone));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据邮箱号查询密码
     *
     * @param uemail
     * @return
     */
    public String loginByEmail(String uemail) {
        try {
            return userDao.selectPwdByUemail(uemail);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 新增用户
     *
     * @param uemail
     * @param uphone
     * @param pwd
     * @return
     */
    public String addUser(String uemail, String uphone, String pwd) {
        try {
            return ResultEntity.success(userDao.addUser(uemail, uphone, pwd));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 修改密码功能
     *
     * @param uemail
     * @param pwd
     * @return
     */
    public String rePwd(String uemail, String pwd) {
        try {
            return ResultEntity.success(userDao.rePwd(uemail, pwd));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 新增用户详情
     *
     * @param uId
     * @param avatar
     * @param weChat
     * @param qq
     * @param sex
     * @param uname
     * @return
     */
    public String addUserDetail(int uId, String avatar, String weChat, String qq, int sex, String uname) {
        try {
            return ResultEntity.success(userDao.addUserDetail(uId, avatar, weChat, qq, sex, uname));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 修改用户详情
     *
     * @param uId
     * @param avatar
     * @param weChat
     * @param qq
     * @param sex
     * @param uname
     * @return
     */
    public String chanceUserDetail(int uId, String avatar, String weChat, String qq, int sex, String uname) {
        try {
            return ResultEntity.success(userDao.chanceUserDetail(uId, avatar, weChat, qq, sex, uname));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }


    /**
     * 获取用户列表
     *
     * @param uName
     * @param start
     * @param cnt
     * @return String
     */
    public String getUserList(String uName, int start, int cnt) {
        try {
            return ResultEntity.success(userDao.getUserDetailList(uName));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 用户详情查询功能
     *
     * @param email
     * @return
     */
    public String getUserDetailList(String email) {
        try {
            return ResultEntity.success(userDao.getUserDetailList(email));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 用户软删除功能
     *
     * @param uId
     * @return
     */
    public String changeIsDel(int uId) {
        try {
            return ResultEntity.success(userDao.changeIsDel(uId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 硬删除用户
     *
     * @param uId
     * @return
     */
    public String delUser(int uId) {
        try {
            return ResultEntity.success(userDao.delUser(uId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
}
